package com.wms.wms.controller.query;

import com.wms.wms.service.ExistingService;
import com.wms.wms.service.InStockService;
import com.wms.wms.service.OutStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

@Controller
@RequestMapping("/query/detail")
public class DetailController {
    @Autowired
    OutStockService outStockService;
    @Autowired
    InStockService inStockService;
    @Autowired
    ExistingService existingService;
    @PostMapping("/out")
    public ModelAndView outStock(@RequestParam(value = "id",required = false) String id,
                                 @RequestParam(value = "type_id",required = false) String typeId,
                                 @RequestParam(value = "b_date",required = false) String bDate,
                                 @RequestParam(value = "e_date",required = false) String eDate
    ){
        ModelAndView modelAndView = new ModelAndView("/query/detail/outResult");
        try {
            if(!bDate.equals("") && !eDate.equals("")){  // 某段时间
                Date b = new Date(new SimpleDateFormat("yyyy-MM-dd").parse(bDate).getTime());
                Date e = new Date(new SimpleDateFormat("yyyy-MM-dd").parse(eDate).getTime());

                if(id.compareTo("000000") != 0) {
                    modelAndView.addObject("id", id);
                    modelAndView.addObject("idList",outStockService.queryByIdAndDate(id,b,e,1));
                }
                else
                {
                    modelAndView.addObject("id","ALL");
                    modelAndView.addObject("idList",outStockService.queryByDate(id,b,e,1));
                }

                if(typeId.compareTo("000") != 0){  // 种类
                    modelAndView.addObject("idt",typeId);
                    modelAndView.addObject("typeList",outStockService.queryByTypeAndDate(typeId,b,e,1));
                }
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return modelAndView;
    }

    @PostMapping("/disposal")
    public ModelAndView disposal(@RequestParam(value = "id",required = false) String id,
                                 @RequestParam(value = "type_id",required = false) String typeId,
                                 @RequestParam(value = "b_date",required = false) String bDate,
                                 @RequestParam(value = "e_date",required = false) String eDate
    ){
        ModelAndView modelAndView = new ModelAndView("/query/detail/disResult");
        try {
            if(!bDate.equals("") && !eDate.equals("")){  // 某段时间
                Date b = new Date(new SimpleDateFormat("yyyy-MM-dd").parse(bDate).getTime());
                Date e = new Date(new SimpleDateFormat("yyyy-MM-dd").parse(eDate).getTime());

                if(id.compareTo("000000") != 0) {
                    modelAndView.addObject("id", id);
                    modelAndView.addObject("idList",outStockService.queryByIdAndDate(id,b,e,0));
                }
                else
                {
                    modelAndView.addObject("id","ALL");
                    modelAndView.addObject("idList",outStockService.queryByDate(id,b,e,0));
                }

                if(typeId.compareTo("000") != 0){  // 种类
                    modelAndView.addObject("idt",typeId);
                    modelAndView.addObject("typeList",outStockService.queryByTypeAndDate(typeId,b,e,0));
                }
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return modelAndView;
    }

    @PostMapping("/in")
    public ModelAndView inStock(@RequestParam(value = "id",required = false) String id,
                                 @RequestParam(value = "type_id",required = false) String typeId,
                                 @RequestParam(value = "b_date",required = false) String bDate,
                                 @RequestParam(value = "e_date",required = false) String eDate
    ){
        ModelAndView modelAndView = new ModelAndView("/query/detail/inResult");
        try {
            if(!bDate.equals("") && !eDate.equals("")){  // 某段时间
                Date b = new Date(new SimpleDateFormat("yyyy-MM-dd").parse(bDate).getTime());
                Date e = new Date(new SimpleDateFormat("yyyy-MM-dd").parse(eDate).getTime());

                if(id.compareTo("000000") != 0) {
                    modelAndView.addObject("id", id);
                    modelAndView.addObject("idList",inStockService.queryByIdAndDate(id,b,e));
                }
                else
                {
                    modelAndView.addObject("id","ALL");
                    modelAndView.addObject("idList",inStockService.queryByDate(id,b,e));
                }

                if(typeId.compareTo("000") != 0){  // 种类
                    modelAndView.addObject("idt",typeId);
                    modelAndView.addObject("typeList",inStockService.queryByTypeAndDate(typeId,b,e));
                }
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return modelAndView;
    }

    @PostMapping("/exist")
    public ModelAndView existing(@RequestParam(value = "id",required = false) String id,
                                @RequestParam(value = "type_id",required = false) String typeId,
                                @RequestParam(value = "b_date",required = false) String bDate,
                                @RequestParam(value = "e_date",required = false) String eDate
    ){
        ModelAndView modelAndView = new ModelAndView("query/detail/exiResult");
        try {

                Date b = new Date(new SimpleDateFormat("yyyy-MM-dd").parse(bDate).getTime());
                Date e = new Date(new SimpleDateFormat("yyyy-MM-dd").parse(eDate).getTime());

                if(id.compareTo("000000") != 0) {
                    modelAndView.addObject("id", id);
                    modelAndView.addObject("idList",existingService.queryByIdAndDate(id,b,e));
                }
                else
                {
                    modelAndView.addObject("id","ALL");
                    modelAndView.addObject("idList",existingService.queryByDate(b,e));
                }

                if(typeId.compareTo("000") != 0){  // 种类
                    modelAndView.addObject("idt",typeId);
                    modelAndView.addObject("typeList",existingService.queryByTypeAndDate(typeId,b,e));
                }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return modelAndView;
    }

    @PostMapping("/outDate")
    public ModelAndView outDate(@RequestParam(value = "id",required = false) String id,
                                 @RequestParam(value = "type_id",required = false) String typeId,
                                 @RequestParam(value = "b_date",required = false) String bDate
    ){
        ModelAndView modelAndView = new ModelAndView("/query/detail/outDResult");
        try {

            Date b = new Date(new SimpleDateFormat("yyyy-MM-dd").parse(bDate).getTime());

            if(id.compareTo("000000") != 0) {
                modelAndView.addObject("id", id);
                modelAndView.addObject("idList",existingService.outDateByIdAndDate(id,b));
            }
            else
            {
                modelAndView.addObject("id","ALL");
                modelAndView.addObject("idList",existingService.outDateByDate(b));
            }

            if(typeId.compareTo("000") != 0){  // 种类
                modelAndView.addObject("idt",typeId);
                modelAndView.addObject("typeList",existingService.outDateByTypeAndDate(typeId,b));
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return modelAndView;
    }

}
